package com.alvis.media.controller.admin;

import com.alvis.media.base.BaseApiController;
import com.alvis.media.base.RestResponse;
import com.alvis.media.service.PythonModelTrainingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 模型训练控制器
 * 提供机器学习模型训练相关的HTTP接口
 * @author
 */
@RestController
@RequestMapping(value = "/api/admin/model-training")
public class ModelTrainingController extends BaseApiController {

    private final PythonModelTrainingService pythonModelTrainingService;
    
    @Autowired
    public ModelTrainingController(PythonModelTrainingService pythonModelTrainingService) {
        this.pythonModelTrainingService = pythonModelTrainingService;
    }
    
    /**
     * 检查Python环境
     * @return 环境检查结果
     */
    @PostMapping(value = "/check-environment")
    public RestResponse<String> checkEnvironment() {
        String result = pythonModelTrainingService.checkPythonEnvironment();
        return RestResponse.ok(result);
    }
    
    /**
     * 使用指定的CSV文件执行模型训练
     * @param csvFilePath CSV文件路径
     * @return 训练结果
     */
    @PostMapping(value = "/train")
    public RestResponse<String> trainModel(@RequestParam String csvFilePath) {
        String result = pythonModelTrainingService.trainModel(csvFilePath);
        return RestResponse.ok(result);
    }
    
    /**
     * 使用指定的CSV文件执行模型训练（带超时设置）
     * @param csvFilePath CSV文件路径
     * @param timeoutSeconds 超时时间（秒）
     * @return 训练结果
     */
    @PostMapping(value = "/train-with-timeout")
    public RestResponse<String> trainModelWithTimeout(
            @RequestParam String csvFilePath,
            @RequestParam(defaultValue = "300") long timeoutSeconds) {
        String result = pythonModelTrainingService.trainModelWithTimeout(csvFilePath, timeoutSeconds);
        return RestResponse.ok(result);
    }
    
    /**
     * 使用示例数据执行模型训练
     * 系统会自动生成示例数据并执行训练
     * @return 训练结果
     */
    @PostMapping(value = "/train-with-example-data")
    public RestResponse<String> trainModelWithExampleData() {
        String result = pythonModelTrainingService.trainModelWithExampleData();
        return RestResponse.ok(result);
    }
    
    /**
     * 获取训练生成的图表文件列表
     * @return 图表文件列表
     */
    @PostMapping(value = "/get-charts")
    public RestResponse<List<String>> getTrainingCharts() {
        List<String> charts = pythonModelTrainingService.getTrainingCharts();
        return RestResponse.ok(charts);
    }
}